package com.meituan.controller;

import com.meituan.dao.OrderRepository;
import com.meituan.dao.StoresDao;
import com.meituan.pojo.dto.OrderMessageDto;
import com.meituan.pojo.entity.Goods;
import com.meituan.pojo.entity.Order;
import com.meituan.pojo.entity.User;
import com.meituan.pojo.resp.BaseOrderResp;
import com.meituan.pojo.resp.BaseResp;
import com.meituan.service.GoodsService;
import com.meituan.service.OrderService;
import com.meituan.service.OrderServiceRR;
import com.meituan.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/shopping")
@Api(description = "用户订单信息")
public class ShoppingController {
    @Autowired
    UserService userService;
    @Autowired
    OrderServiceRR orderServiceRR;
    @Autowired
    OrderService orderService;
    @Autowired
    GoodsService goodsService;


    @GetMapping ("addGood")
    @ApiOperation(value = "添加商品到购物车，sid-商家id，gid-商品id，gnum-数量")
    public BaseResp addGood(int sid,int gid,int gnum){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //判断商品库存是否足够
            BaseResp byGid = goodsService.findByGid(gid);
            Goods data = (Goods) byGid.getData();
            if (data.getGnumber()==0){
                return new BaseResp(0,"商品库存不足",null);
            }
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            //判断此用户此商家此商品是否存在，如果存在只增加商品数量
            List<BaseOrderResp> byAllId = orderService.findByAllId(uid, sid, gid);
            if (byAllId!=null&&byAllId.size()!=0){
                BaseOrderResp order = byAllId.get(0);
                if (order.getState()==1){
                    int gnum_new = order.getGnum()+gnum;

//                    if (gnum_new>data.getGnumber()){
//                        return new BaseResp(0,"商品库存不足",null);
//                    }
                    boolean b = orderService.updateGnum(order.getOid(), gnum_new);
                    if (b){
                        OrderMessageDto orderMessageDto =  orderService.fingOneOrder(uid,sid,gid);
                        orderMessageDto.setMoney(orderMessageDto.getPrice()*orderMessageDto.getGnum());
                        return new BaseResp(1,"success",orderMessageDto);
//                        return new BaseResp().SUCCESS();
                    }
                    return new BaseResp(0,"添加失败",null);
                }
            }
            //创建订单对象
            Order order = new Order();
            order.setOid(0);
            order.setSid(sid);
            order.setGid(gid);
            order.setGnum(gnum);
            order.setUid(uid);
            order.setOtime(new Date());
            order.setState(1);
            Order save = orderServiceRR.save(order);
            //创建订单信息返回
//            OrderMessageDto orderMessageDto = new OrderMessageDto();
            OrderMessageDto orderMessageDto =  orderService.fingOneOrder(uid,sid,gid);
            orderMessageDto.setMoney(orderMessageDto.getPrice()*orderMessageDto.getGnum());
            System.out.println(orderMessageDto);
            return new BaseResp(1,"success",orderMessageDto);

        }
        return new BaseResp(0,"用户未登陆",null);
    }

    @GetMapping ("delGood")
    @ApiOperation(value = "删除购物车商品，sid-商家id，gid-商品id")
    public BaseResp delGood(int sid,int gid){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            //判断此用户此商家此商品是否存在
            List<BaseOrderResp> byAllId = orderService.findByAllId(uid, sid, gid);
            if (byAllId!=null&&byAllId.size()!=0){
                BaseOrderResp order = byAllId.get(0);
                //如果订单有效
                if (order.getState()==1){
                    boolean b = orderService.deleteByOid(order.getOid());
                    if (b){
                        return new BaseResp().SUCCESS();
                    }
                    return new BaseResp(0,"删除失败，请重试",null);
                }
            }
            return new BaseResp(0,"此订单不存在",null);

        }
        return new BaseResp(0,"用户未登陆",null);
    }

    @GetMapping ("upOneGood")
    @ApiOperation(value = "商品数量+1，，sid-商家id，gid-商品id")
    public BaseResp upOneGood(int sid,int gid){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            //判断此用户此商家此商品是否存在
            List<BaseOrderResp> byAllId = orderService.findByAllId(uid, sid, gid);
            if (byAllId!=null&&byAllId.size()!=0){
                BaseOrderResp order = byAllId.get(0);
                //如果订单有效
                if (order.getState()==1){
                    int numUpOne = order.getGnum() + 1;
                    boolean b = orderService.updateGnum(order.getOid(),numUpOne);
                    if (b){
                        return new BaseResp().SUCCESS();
                    }
                    return new BaseResp(0,"修改失败，请重试",null);
                }
            }
            return new BaseResp(0,"此订单不存在",null);

        }
        return new BaseResp(0,"用户未登陆",null);
    }

    @GetMapping ("downOneGood")
    @ApiOperation(value = "商品数量-1，sid-商家id，gid-商品id")
    public BaseResp downOneGood(int sid,int gid){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            //判断此用户此商家此商品是否存在
            List<BaseOrderResp> byAllId = orderService.findByAllId(uid, sid, gid);
            if (byAllId!=null&&byAllId.size()!=0){
                BaseOrderResp order = byAllId.get(0);
                //如果订单有效
                if (order.getState()==1){
                    if (order.getGnum() == 1){
                        boolean b = orderService.deleteByOid(order.getOid());
                        if (b){
                            return new BaseResp().SUCCESS();
                        }
                        return new BaseResp(0,"删除失败，请重试",null);
                    }
                    int numUpOne = order.getGnum() - 1;
                    boolean b = orderService.updateGnum(order.getOid(),numUpOne);
                    if (b){
                        return new BaseResp().SUCCESS();
                    }
                    return new BaseResp(0,"修改失败，请重试",null);
                }
            }
            return new BaseResp(0,"此订单不存在",null);

        }
        return new BaseResp(0,"用户未登陆",null);
    }

    @GetMapping ("findAllGood")
    @ApiOperation(value = "购物车全查，不需要传惨")
    public BaseResp findAllGood(){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            List<StoresDao> shopping = orderService.findShopping(uid);
            return new BaseResp(1,"success",shopping);
        }
        return new BaseResp(0,"用户未登陆",null);
    }
    @GetMapping ("historyOrder")
    @ApiOperation(value = "查询历史订单，不需要传惨")
    public BaseResp historyOrder(){
        //判断用户是否登陆
        Subject subject = SecurityUtils.getSubject();
        boolean islogin = subject.isAuthenticated();
        if (islogin){
            //获取用户id
            String userName = (String)subject.getPrincipal();
            System.out.println(userName);
            User userName1 = userService.findUserByUserName(userName);
            int uid = userName1.getId();
            List<StoresDao> shopping = orderService.findHistoryShopping(uid);
            return new BaseResp(1,"success",shopping);
        }
        return new BaseResp(0,"用户未登陆",null);
    }
//    buyGood
//    payComplete

}
